package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

///system/role/list.do
@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private ModuleService moduleService;


    @RequestMapping(value = "/list",name = "分页展示角色列表数据")
    public String findAll(@RequestParam(value = "page",required=false,defaultValue ="1" )Integer pageNum,
                          @RequestParam(required=false,defaultValue ="10" )Integer pageSize){

//        分页查询角色数据时要区分是哪个企业的
        PageInfo pageInfo = roleService.findPage(getCompanyId(),pageNum,pageSize);
        request.setAttribute("page",pageInfo); //这个“page” 固定不能修改 因为页面上要的就是page这个变量名
        return "system/role/role-list";
    }
    @RequestMapping(value = "/toAdd",name = "进入到新增角色的页面")
    public String toAdd(){

        return "system/role/role-add";
    }


    @RequestMapping(value = "/edit",name = "保存角色的方法")
    public String edit(Role role){
//        判断role中id是否为空，如果为空就是新增反之就是修改
        if(StringUtils.isEmpty(role.getId())){//如果为空
            String id = UUID.randomUUID().toString();
            role.setId(id);
            role.setCompanyId(getCompanyId());
            role.setCompanyName(getCompanyName());
            roleService.save(role);
        }else{
            roleService.update(role);
        }
        return "redirect:/system/role/list.do"; //一定要重定向到列表页面
    }

    @RequestMapping(value = "/toUpdate",name = "进入到修改角色的页面")
    public String toUpdate(String id){
        Role role = roleService.findById(id);
        request.setAttribute("role",role);
        return "system/role/role-add";
    }

    @RequestMapping(value = "/delete",name = "删除角色的方法")
    public String delete(String id){
        roleService.delete(id);
        return "redirect:/system/role/list.do"; //一定要重定向到列表页面
    }

/*
    @RequestMapping(value = "/roleModule",name = "进入到角色分配权限的页面--原方法")
    public String roleModule(String roleid){
        Role role = roleService.findById(roleid);
        request.setAttribute("role",role);
        return "system/role/role-module"; //一定要重定向到列表页面
    }*/


    @RequestMapping(value = "/roleModule",name = "进入到角色分配权限的页面")
    public String roleModule(String roleid){
        Role role = roleService.findById(roleid);
        request.setAttribute("role",role);


        //        查询当前角色下有哪些权限的id（菜单或模块）数据  select module_id from pe_role_module where role_id=?
        List<String> moduleIdList= roleService.findModuleIdsByRoleId(roleid);

        List<Module> allModuleList = moduleService.findAll(); //查询所有的模块菜单数据
       /* [ { id:11, pId:1, name:"随意勾选 1-1"},
        { id:111, pId:11, name:"随意勾选 1-1-1"},
        { id:112, pId:11, name:"随意勾选 1-1-2"}]*/

        List<Map> moduleMapList = new ArrayList<>();
        Map map = null;
        for (Module module : allModuleList) {
            map = new HashMap<>();
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());

//            当前角色拥有的权限id集合 是否 包含 module.getId()
            if(moduleIdList.contains(module.getId())) {
                map.put("checked", true); // 当前角色拥有的权限应该有这个checked属性
            }
            moduleMapList.add(map);
        }

        String jsonString = JSON.toJSONString(moduleMapList);

        request.setAttribute("moduleMapList",jsonString);

        return "system/role/role-module"; //一定要重定向到列表页面
    }

    @RequestMapping(value = "/getZtreeNodes",name = "获取ztree的节点数据")
    @ResponseBody // 把返回的数据转成json 并且回显到浏览器
    public List<Map> getZtreeNodes(String roleid){
//        查询当前角色下有哪些权限的id（菜单或模块）数据  select module_id from pe_role_module where role_id=?
        List<String> moduleIdList= roleService.findModuleIdsByRoleId(roleid);

        List<Module> allModuleList = moduleService.findAll(); //查询所有的模块菜单数据
       /* [ { id:11, pId:1, name:"随意勾选 1-1"},
        { id:111, pId:11, name:"随意勾选 1-1-1"},
        { id:112, pId:11, name:"随意勾选 1-1-2"}]*/

       List<Map> moduleMapList = new ArrayList<>();
       Map map = null;
        for (Module module : allModuleList) {
            map = new HashMap<>();
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());

//            当前角色拥有的权限id集合 是否 包含 module.getId()
           if(moduleIdList.contains(module.getId())) {
               map.put("checked", true); // 当前角色拥有的权限应该有这个checked属性
           }
            moduleMapList.add(map);
        }
        return  moduleMapList;
    }



    @RequestMapping(value = "/updateRoleModule",name = "保存角色分配权限方法")
    public String updateRoleModule(String roleid ,String moduleIds){ //2,201,202
        roleService.updateRoleModule(roleid,moduleIds);
        return "redirect:/system/role/list.do"; //一定要重定向到列表页面
    }

}
